<?php

	class GP_page_login extends GP_controleur
	{
		var $controleur;
		
		function __construct()
		{
			global $page;
			$this -> controleur = parent::getInstance($page);
		}
		
		function connexion($identifiant, $mdp)
		{
			global $gp_db;
			GP_ActiveRecord::setDefaultDBConnection($gp_db);
			
			$users = new GP_ActiveRecord('_bo_users');
			
			$select = array(
				'id',
				'langue',
				'login',
				'nom',
				'prenom',
				'role',
				'courriel'
			);
			$tab = $users -> get($select, "actif = 1 && login = '" . addslashes($identifiant) . "' && mdp = '" . md5($mdp) . "' LIMIT 1");
			foreach ($tab as $user)
			{
				extract($user);
				if ($id > 0)
				{
					s("bo_user", $id);
					s("bo_user_lng", $langue);
					s("bo_user_login", $login);
					s("bo_user_nom", $nom);
					s("bo_user_prenom", $prenom);
					s("bo_user_courriel", $courriel);
					s("bo_user_role", $role);
					s("applng_bo", $langue);
					return true;
				}
			}
			return false;
		}
		
		function afficher()
		{			
			global $page, $urirw;
			if (strlen($urirw))
			{
				$pagerw = fakeurl($urirw);
				$htmltmp = $this -> controleur -> htmlrw('header');
				$htmltmp .= $this -> controleur -> htmlrw($page);
				$htmltmp .= $this -> controleur -> htmlrw('footer');
				savefile($pagerw, $htmltmp);
				if (!IS_IE)
					header("HTTP/1.1 301 Moved Permanently");
				include($urirw);
			}
			else
			{
				$this -> controleur -> add_html('header');
				$this -> controleur -> add_html($page);
				$this -> controleur -> add_html('footer');
			}
		}
		
		function setvar($var, $val)
		{
			global $urirw;
			if (strlen($urirw))
				$this -> $var = $val;
			else
				$this -> controleur -> $var = $val;
		}
	}
	
	$gp_request = GP_secure::getInstance();
	$page_login = new GP_page_login;
	$identifiant = $gp_request -> Get('identifiant');
	$mdp = $gp_request -> Get('mdp');
	
	if (strlen($identifiant) && strlen($mdp))
	{
		$erreur = false;
		$connexion = $page_login -> connexion($identifiant, $mdp);
		if (!$connexion)
			$erreur = true;
		else
			header('Location: ' . rw('accueil'));
		$page_login -> setvar('erreur', $erreur);
	}
	
	$page_login -> setvar('page', $page);
	$page_login -> setvar('identifiant', $identifiant);
	$page_login -> setvar('mdp', $mdp);
	$page_login -> setvar('titre', 'Page inconnue');
	$page_login -> afficher();
?>